﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Gakita.View
{
    class MenuView
    {
        private SpriteBatch _spriteBatch;
        private KeyboardState _oldKeyboardState;
        private SpriteFont _menuFont;

        public enum UserInput
        {
            ChooseCurrent,
            ChangeUp,
            ChangeDown,
            Nothing,
        }

        public MenuView(GraphicsDeviceManager aManager, ContentManager aContentManager, SpriteBatch _aSpriteBatch)
        {
            _spriteBatch = _aSpriteBatch;
            _oldKeyboardState = Keyboard.GetState();
            _menuFont = aContentManager.Load<SpriteFont>("MenuFont");
        }

        internal void DrawMainMenu(GraphicsDevice aGraphicsDevice, Model.MenuModel.MenuOptions[] aMenuOptions, Model.MenuModel.MenuOptions aSelectedMenuOption, bool aGameIsRunning)
        {
            aGraphicsDevice.Clear(new Microsoft.Xna.Framework.Color(142, 255, 255));
            Vector2 viewportSize = new Vector2(aGraphicsDevice.Viewport.Width, aGraphicsDevice.Viewport.Height);

            _spriteBatch.Begin();
            foreach(Model.MenuModel.MenuOptions menuOption in aMenuOptions)
            {
                //Draw the selected alternative in red
                if ((int)menuOption == (int)aSelectedMenuOption)
                {
                    DrawMenuOption(viewportSize, menuOption, Color.Red, aGameIsRunning);
                }
                //Draw the other alternatives
                else
                {
                    DrawMenuOption(viewportSize, menuOption, Color.Black, aGameIsRunning);
                }
            }
            _spriteBatch.End();
        }

        internal void DrawMenuOption(Vector2 aViewportSize, Model.MenuModel.MenuOptions aMenuOption, Color aColor, bool aGameIsRunning)
        {
            string menuString = null;

            switch (aMenuOption)
            {
                case Model.MenuModel.MenuOptions.NewGame:
                    if (aGameIsRunning == true)
                    {
                        menuString = "Resume Game";
                    }
                    else
                    {
                        menuString = "New Game";
                    }
                    break;
                case Model.MenuModel.MenuOptions.HighScore:
                    menuString = "High Score";
                    break;
                case Model.MenuModel.MenuOptions.Options:
                    menuString = "Options";
                    break;
                case Model.MenuModel.MenuOptions.ExitGame:
                    menuString = "Exit Game";
                    break;
            }
            Vector2 textSize = _menuFont.MeasureString(menuString);
            Vector2 textPosition = new Vector2((aViewportSize.X / 2 - textSize.X/2),((aViewportSize.Y / 5) * (int)aMenuOption - textSize.Y/2));

            _spriteBatch.DrawString(_menuFont, menuString, textPosition, aColor);
        }

        internal UserInput GetUserInput()
        {  
            KeyboardState newKeyboardState = Keyboard.GetState();

            UserInput ret = UserInput.Nothing;

            if (_oldKeyboardState.IsKeyDown(Keys.Space) && newKeyboardState.IsKeyUp(Keys.Space) || _oldKeyboardState.IsKeyDown(Keys.Enter) && newKeyboardState.IsKeyUp(Keys.Enter))
            {
                ret = UserInput.ChooseCurrent;
            }
            else if (_oldKeyboardState.IsKeyDown(Keys.Down) && newKeyboardState.IsKeyUp(Keys.Down))
            {
                ret = UserInput.ChangeDown;
            }
            else if (_oldKeyboardState.IsKeyDown(Keys.Up) && newKeyboardState.IsKeyUp(Keys.Up))
            {
                ret = UserInput.ChangeUp;
            }

            _oldKeyboardState = newKeyboardState;

            return ret;
        }

    }
}
